const Controller = require("egg").Controller;

// 校验规则
const vBook = {
  book_name: "string",
  book_cate: "string",
};

const vList = {
  pagesize: "number",
  pagenum: "number",
};

const vStorage = {
    name: 'string',
    author: 'string',
    cateid: 'string',
    publishdate: 'string',
}

class BookController extends Controller {
  // 图书入库
  async storage() {
    const { ctx, service } = this;
    ctx.validate(vStorage, ctx.request.body)

    const res = await service.books.book.storage(ctx.request.body)
    if(res.affectedRows === 1){
        ctx.body = ctx.success('新增图书成功');
    }else {
        ctx.body = ctx.fail('新增图书失败')
    }
  }
  // 图书重新入库
  async reStorage() {
    const { ctx, service } = this;
    const res = await service.books.book.reStorage(ctx.request.body)
    if(res.affectedRows === 1){
        ctx.body = ctx.success('新增图书成功');
    }else {
        ctx.body = ctx.fail('新增图书失败')
    }
  }
  // 图书列表
  async list() {
    const { ctx, service } = this;
    ctx.validate(vList, ctx.request.body);
    const res = await service.books.book.list(ctx.request.body);
    ctx.body = ctx.list("获取图书列表成功", {
      data: res.data,
      total: res.total,
    });
  }
  // 图书已删除列表
  async listDel() {
    const { ctx, service } = this;
    ctx.validate(vList, ctx.request.body);
    const res = await service.books.book.listDel(ctx.request.body);
    ctx.body = ctx.list("获取图书列表成功", {
      data: res.data,
      total: res.total,
    });
  }
  // 图书删除
  async delete() {
    const { ctx, service } = this;
    const res = await service.books.book.delete(ctx.params)
    if(res.affectedRows === 1){
        ctx.body = ctx.success('删除图书成功');
    }else {
        ctx.body = ctx.fail('删除图书失败')
    }
  }
  // 图书删除
  async deleteRel() {
    const { ctx, service } = this;
    const res = await service.books.book.deleteRel(ctx.params)
    if(res.affectedRows === 1){
        ctx.body = ctx.success('删除图书成功');
    }else {
        ctx.body = ctx.fail('删除图书失败')
    }
  }
  // 图书更新
  async update() {
    const { ctx, service } = this;
    vStorage.id = 'string'
    ctx.validate(vStorage, ctx.request.body)
    const res = await service.books.book.update(ctx.request.body)
    if(res.affectedRows === 1){
        ctx.body = ctx.success('修改图书成功');
    }else {
        ctx.body = ctx.fail('修改图书失败')
    }
  }
  // 图书上架
  async shlef() {
    const { ctx, service } = this;
    const res = await service.books.book.shlef(ctx.request.body)
    if(res.affectedRows === 1){
        ctx.body = ctx.success('上架成功');
    }else {
        ctx.body = ctx.fail('上架失败')
    }
  }
}

module.exports = BookController;
